package co.fitstart.weapp.api.service.impl;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dao;

import co.fitstart.entity.user.Student;
import co.fitstart.entity.user.User;
import co.fitstart.entity.user.agent.Agent;
import co.fitstart.entity.user.agent.AgentDiscount;
import co.fitstart.entity.user.agent.AgentRecord;
import co.fitstart.entity.user.agent.OfflineAgent;
import co.fitstart.entity.user.agent.RecordType;
import co.fitstart.weapp.api.dao.UserAgentDao;
import co.fitstart.weapp.api.service.UserAgentService;

/**
 * 
 * @author dan
 *
 */
@Service("userAgentService")
public class UserAgentServiceImpl implements UserAgentService{
    
    @Dao
    private UserAgentDao userAgentDao;
    
    @Autowired
    private GenericDaoSupport genericDaoSupport;
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.weapp.api.service.UserAgentService#load(co.fitstart.entity.user.User)
     */
    public Agent load(User user) {
        return userAgentDao.load(user);
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.weapp.api.service.UserAgentService#listOfflineAgent(co.fitstart.entity.user.Student)
     */
    public List<OfflineAgent> listOfflineAgent(Student student) {
        return userAgentDao.listOfflineAgent(student);
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.weapp.api.service.UserAgentService#listDiscount(co.fitstart.entity.user.agent.Agent)
     */
    public List<AgentDiscount> listDiscount(Agent agent) {
        return userAgentDao.listAgentDiscount(agent);
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.weapp.api.service.UserAgentService#listAgentRecord(co.fitstart.entity.user.agent.Agent)
     */
    public List<AgentRecord> listAgentRecord(Agent agent) {
        return userAgentDao.listAgentRecord(agent);
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.weapp.api.service.UserAgentService#editAccount(co.fitstart.entity.user.agent.Agent, java.lang.String, java.lang.String)
     */
    public void editAccount(Agent agent, String account, String realName) {
        genericDaoSupport.update(agent.editAccount(account,realName));
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.weapp.api.service.UserAgentService#withdraw(co.fitstart.entity.user.agent.Agent, java.math.BigDecimal)
     */
    public void withdraw(Agent agent, BigDecimal offer) {
        
        int offerFen = offer.multiply(new BigDecimal(100)).intValue();
        // save agent record
        AgentRecord agentRecord = new AgentRecord(agent, RecordType.WITHDRAWING, offerFen);
        genericDaoSupport.save(agentRecord);
        
        // update agent withdrawl price
        genericDaoSupport.update(agent.withdraw(offerFen));
        
    }
    

}
